package com.erastep.util
{
	import mx.controls.DateField;
	import mx.validators.DateValidator;
	
	public class DateTimeUtils
	{
		public function DateTimeUtils()
		{
			
		}

		/**
		 * 计算两个时间相差多少天 d1-d2
		 * @param d1
		 * @param d2
		 */
		public static function diffDate(d1:Date, d2:Date):int
		{
			var diff:Number=d1.valueOf() - d2.valueOf();
			return int(diff / 24 / 60 / 60 / 1000);
		} 
		
		/**
		 * 日期的前一天
		 * @param date
		 * @return 
		 */
		public static function prevDate(date:Date):Date
		{
			//new Date(
			return addDate(date, -1);
		}
		/**
		 * 日期的后一天
		 * @param date
		 * @return 
		 */
		public static function nextDate(date:Date):Date
		{
			return addDate(date, 1);
		}
		
		/**
		 * 日期添加N天,只支持短时间，即年月日 
		 * @param date
		 * @param days
		 * @return 
		 */
		public static function addDate(date:Date, days:int):Date
		{
			return new Date(date.fullYear, date.month, date.date + days);
		}
		
		/**
		 * 日期添加N月，只至支持短时间，即年月日 
		 * @param date
		 * @param months
		 * @return 
		 */
		public static function addMonth(date:Date, months:int):Date
		{
			return new Date(date.fullYear, date.month + months, date.date);
		}
		
		/**
		 * 日期添加N年，只至支持短时间，即年月日 
		 * @param date
		 * @param yeas
		 * @return 
		 */
		public static function addYear(date:Date,yeas:int):Date
		{
			return new Date(date.fullYear + yeas, date.month, date.date);
		}
		
		/**
		 * 传一个YYYY-MM-DD HH:NN:SS类似字符串
		 * 解析成一个Date对象
		 */ 
		public static function parseString2DateTime(str:String):Date
		{
			var arr:Array=str.split(" ");
			var dateArr:Array=arr[0].toString().split("-");
			var timeArr:Array=arr[1].toString().split(":");
			var date:Date=new Date(Number(dateArr[0]),Number(dateArr[1])-1,Number(dateArr[2]),Number(timeArr[0]),Number(timeArr[1]),Number(timeArr[2]),0)
			return date;
		}
		
		/**
		 * 传一个YYYY-MM-DD类似字符串
		 * 解析成一个Date对象
		 */ 
		public static function parseString2Date(str:String):Date
		{
			var dateArr:Array=str.split("-");
			var date:Date=new Date(Number(dateArr[0]), Number(dateArr[1])-1, Number(dateArr[2]), 0, 0, 0,0)
			return date;
		}
		
		/**
		 * 返回当天的短日期  YYYY-MM-DD
		 */
		public static function Today():Date
		{
			var date:Date = new Date();
			return new Date(date.fullYear, date.month, date.date);
		}
			
		/**
		 * 返回当天的日期字符串 YYYY-MM-DD
		 */
		public static function TodayString():String
		{
			return FormatDate(new Date(), false);
		}
		
		/**
		 * Date转换为字符串  useTime 默认为 false</p>
		 *  useTime=false 返回 YYYY-MM-DD  ; useTime=true 返回 YYYY-MM-DD HH:NN:SS
		 */
		public static function FormatDate(value:Date, useTime:Boolean = false):String
		{
			if (value)
			{
				if (useTime)
				{
					return value.fullYear.toString() + "-" + addLeadingZero(value.month + 1) + "-" + addLeadingZero(value.date) 
						+ " " + addLeadingZero(value.hours) + ":" + addLeadingZero(value.minutes) + ":" + addLeadingZero(value.seconds); 
				}
				else
				{
					return value.fullYear.toString() + "-" + addLeadingZero(value.month + 1) + "-" + addLeadingZero(value.date);
				}
			}
			return null;
		}
		
		/**
		 * Date转换为字符串  返回 YYYY-MM-DD HH:NN
		 */
		public static function FormatDate2(value:Date):String
		{
			if (value)
			{
				return value.fullYear.toString() + "-" + addLeadingZero(value.month + 1) + "-" + addLeadingZero(value.date) 
					+ " " + addLeadingZero(value.hours) + ":" + addLeadingZero(value.minutes); 
			}
			return null;
		}
		
		/**
		 * Date转换为字符串  useTime 默认为 false</p>
		 *  useTime=false 返回 YYYY年MM月DD日  ; useTime=true 返回 YYYY年MM月DD日 HH时NN分SS秒
		 */
		public static function FormatDateCHN(value:Date, useTime:Boolean = false):String
		{
			if (value)
			{
				if (useTime)
				{
					return value.fullYear.toString() + "年" + addLeadingZero(value.month + 1) + "月" + addLeadingZero(value.date) 
						+ "日 " + addLeadingZero(value.hours) + "时" + addLeadingZero(value.minutes) + "分" + addLeadingZero(value.seconds) + "秒"; 
				}
				else
				{
					return value.fullYear.toString() + "年" + addLeadingZero(value.month + 1) + "月" + addLeadingZero(value.date) + "日";
				}
			}
			return null;
		}
		
		public static function addLeadingZero(n:Number):String
		{
			var out:String = String(n);
			if(out.length < 2)
			{
				out = "0" + out;
			}
			return out;
		}
		
		/**
		 * 字符串转换为Date类型</p>
		 * 格式：YYYYMMDD => date	  YYYYMMDDHHMMSS => datetime
		 */
		public static function CompactStringToDate(s:String):Date
		{
			if (s && s.length >= 8)
			{
				var date:Date;
				var numYear:Number = Number(s.substr(0, 4));
				var numMonth:Number = Number(s.substr(4,2)) - 1;
				var numDate:Number = Number(s.substr(6, 2));
				if (s.length > 8)
				{
					var numHours:Number = Number(s.substr(8, 2));
					var numMinutes:Number = Number(s.substr(10, 2));
					var numSeconds:Number = Number(s.substr(12, 2));
					var numMilliseconds:Number = Number(s.substr(14, 3));
					date = new Date(numYear, numMonth, numDate, numHours, numMinutes, numSeconds, numMilliseconds);
				}
				else
				{
					date = new Date(numYear, numMonth, numDate, 0, 0, 0, 0);
				}
				return date;
			}
			return null;
		}
		
		/**
		 * Date类型转换为字符串  useTime 默认为 false</p>
		 * useTime=false 返回 YYYYMMDD  ; useTime=true 返回 YYYYMMDDHHMMSS
		 */
		public static function DateToCompactString(value:Date, useTime:Boolean = false):String
		{
			if (value)
			{
				if (useTime)
				{
					return value.fullYear.toString() + addLeadingZero(value.month + 1) + addLeadingZero(value.date) 
						+ addLeadingZero(value.hours) + addLeadingZero(value.minutes) + addLeadingZero(value.seconds);
				}
				else
				{
					return value.fullYear.toString() + addLeadingZero(value.month + 1) + addLeadingZero(value.date); 
				}
			}
			return null;
		}
		
		/**
		 * 日期控件设置
		 */
		public static function InitDateField(editor:DateField):void
		{
			editor.formatString = "YYYY-MM-DD";
			editor.restrict = "0-9\\-"; 
			editor.yearNavigationEnabled = false;
			editor.editable = true;
			
			//editor.monthSymbol = "月";
			//editor.dayNames = ["日","一","二","三","四","五","六"]; 
			//editor.monthNames = ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"];  
			//editor.yearSymbol = "年";				
		}
		
		public static function DateEqual(date1:Date, date2:Date):Boolean
		{
			return (date1.fullYear == date2.fullYear) && (date1.month == date2.month) && (date1.date == date2.date); 
		}

		public static function DateTimeEqual(date1:Date, date2:Date):Boolean
		{
			return (date1.fullYear == date2.fullYear) && (date1.month == date2.month) && (date1.date == date2.date)
				&& (date1.hours == date2.hours) && (date1.minutes == date2.minutes) && (date1.seconds == date2.seconds) && (date1.milliseconds == date2.milliseconds); 
		}
		
		public static function IsDate(s:String):Boolean
		{
			var datev:DateValidator = new DateValidator();
			datev.inputFormat = "YYYY-MM-DD";
			var errors:Array = datev.validate(s, true).results;
			if (errors && errors.length != 0)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
	}
}